package com.cim120.commons.cip.process;

import com.cim120.commons.cip.constant.ImageConstant;
import com.cim120.commons.cip.constant.ImageEnum;
import com.cim120.commons.cip.model.Image;
import com.cim120.commons.cip.utils.GraphicsMagickUtils;
import com.cim120.commons.cip.utils.ImageUtils;
import com.cim120.commons.utils.FileUtil;

import java.io.File;
import java.util.Map;

/**
 * Created by tonydeng on 16/4/27.
 */
public interface ImageProcess {
    /**
     * 根据项目和类型进行切图对文件进行
     *
     * @param project
     * @param type
     * @param image
     * @return
     */
    Map<String, Image> cropImageByFile(ImageEnum.Project project, ImageEnum.Type type, File image);

    /**
     * 图片质量处理，并返回处理后的图片
     *
     * @param image
     * @return
     */
    default File qualitry(File image) {
        if(image.exists()){
            String extension = ImageUtils.getExtension(image.getPath());
            if(!ImageUtils.isDefaultExtension(extension)){
                File desc = new File(FileUtil.replaceFilePostfix(image,ImageConstant.DEFAULT_FORMAT));
                boolean cps = FileUtil.copyFile(image,desc);
                if(cps){
                    FileUtil.deleteFile(image.getPath());
                    image = desc;
                }
            }
            GraphicsMagickUtils.qualityByOrientaion(image.getAbsolutePath(),image.getAbsolutePath(), ImageConstant.DEF_IMAGE_QUALITY);
        }
        return image;
    }
}
